/*
 * Modified Version 
 * Author: rendy.jr
 * Date: Jun 10, 2011
 * 6.005 Elements of Software Construction
 * Rendy Bambang Junior
 * rendy.jr@gmail.com
 * 
 */

class Scrambler {
	
	/* reverse input char by char */
    static String reverse(String input) {
        // YOUR CODE HERE
    	String temp = new String();
    	for(int i = 0; i < input.length(); i++){
    		temp = input.charAt(i) + temp; 
    	}
        return temp;
    }    
    
    /* reverse input sentence word by word */
	static String reverseWords(String input) {
        // YOUR CODE HERE
		String words[] = input.split(" ");
		String temp = new String();
    	for(int i = words.length-1; i >= 0; i--){
    		if(i==words.length-1)
    			temp = words[i];    		
    		else
    			temp = temp + " " + words[i];
    	}
		return temp;
	}

	/* return whether input palindrome by word*/
    static boolean isWordPalindrome(String input) {
        // YOUR CODE HERE
    	if(input!=null){
	    	String words[] = input.split(" ");
	    	boolean isPalindrom = true;
	    	for(int i = 0; i < words.length/2; i++){
	    		if(!words[i].equals(words[words.length-1-i])){
	    			isPalindrom = false;
	    			break;
	    		}
	    	}
	    	
	        return isPalindrom;
    	}
    	else
    		return false;
    }
    
    /* driver */
	public static void main(String args[]) {
        String input, output;
        
        // reverse
        input = "Three blind mice";
        output = reverse(input);
        System.out.println("Should be: ecim dnilb eerhT");
        System.out.println("Actual is: " + output);

        input = "level madam level";
        output = reverse(input);
        System.out.println("\nShould be: level madam level");
        System.out.println("Actual is: " + output);
        
        // reverseWords
		input = "To be or not to be, that is the question.";
		output = reverseWords(input);
        System.out.println("\nShould be: question. the is that be, to not or be To");
        System.out.println("Actual is: " + output);

		input = "Stressed spelled backwards is Desserts";
		output = reverseWords(input);
        System.out.println("\nShould be: Desserts is backwards spelled Stressed");
        System.out.println("Actual is: " + output);

        // isWordPalindrome
        input = "forever eating cheese";
        boolean boolOutput = isWordPalindrome(input);
        System.out.println("\n" + input + (boolOutput ? " is " : " is not ") + "a word palindrome");

        input = "fall leaves when leaves fall";
        boolOutput = isWordPalindrome(input);
        System.out.println("\n" + input + (boolOutput ? " is " : " is not ") + "a word palindrome");
        
        input = null;
        boolOutput = isWordPalindrome(input);
        System.out.println("\n" + input + (boolOutput ? " is " : " is not ") + "a word palindrome");
        
    }

}
